package com.sh.fmbackend.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class OSSService {

    @Autowired
    private OSS ossClient;

    @Autowired
    private String bucketName;

    @Value("${aliyun.oss.filePrefix}")
    private String filePrefix;  // 注入配置中的前缀



    public String uploadFile(MultipartFile file) throws IOException {
        // 获取文件名
        String fileName = filePrefix+file.getOriginalFilename();

        // 上传文件到 OSS
            ossClient.putObject(new PutObjectRequest(bucketName, fileName, file.getInputStream()));

        // 返回文件访问路径
        return "http://" + bucketName + "." + "oss-cn-wuhan-lr.aliyuncs.com" + "/" + fileName;
    }
}
